library(ggplot2)
library(corrplot)
tcgavn <- read.table("tcga交集基因fpkms.txt",sep = "\t",check.names = F,stringsAsFactors = F,header = T)
tcgaupr <- read.table("tcgauprmt-er.txt",sep = "\t",check.names = F,stringsAsFactors = F,header = T)
tcgaupr[,1] <- substr(tcgaupr[,1], 1, 16)
dup_rows <- tcgaupr[duplicated(tcgaupr[, 1]), 1] ##因为输入数据里行名有重复，这四步为找到重复行名
dup_suffix <- ave(dup_rows, dup_rows, FUN = seq_along)
tcgaupr[duplicated(tcgaupr[, 1]), 1] <- paste0(dup_rows, ".", dup_suffix)
rownames(tcgaupr) <- tcgaupr[,1]
tcgaupr <- tcgaupr[,-1]
tcgavn <- t(tcgavn)
comgene <- intersect(rownames(tcgaupr),rownames(tcgavn))
tcgavn <- tcgavn[comgene,]
tcgaupr <- tcgaupr[comgene,]

tcgauprmt_vn=cbind(tcgaupr, tcgavn)
library(Hmisc)#加载包
res2 <- rcorr(as.matrix(tcgauprmt_vn)) ##可以看到这个list里包含三个，r为相关性分数，p为pvalue
r <- res2$r
p <- res2$P

vn_r <- r[colnames(tcgavn),]
vn_r <- vn_r[,colnames(tcgaupr)]
vn_p <- p[colnames(tcgavn),]
vn_p <- vn_p[,colnames(tcgaupr)]
library(tidyr)
library(dplyr)
library(tibble)
# 假设你的数据框名为df
df <- as.data.frame(vn_r) %>%           # 将vn_r转换为数据框
  rownames_to_column("Gene") %>%        # 将行名转换为Gene列
  pivot_longer(-Gene,                   # 将Gene列之外的所有列都转换为长格式
               names_to = "Variable",   # 新列的名称，用来存储原列名
               values_to = "Value")     # 新列的名称，用来存储原数值

df2 <- as.data.frame(vn_p) %>%           # 将vn_r转换为数据框
  rownames_to_column("Gene") %>%        # 将行名转换为Gene列
  pivot_longer(-Gene,                   # 将Gene列之外的所有列都转换为长格式
               names_to = "Variable",   # 新列的名称，用来存储原列名
               values_to = "Value")     # 新列的名称，用来存储原数值
mergedf <- cbind(df,df2)
mergedf <- mergedf[,-4]
mergedf <- mergedf[,-4]
# 查看转换后的数据框
head(df)



write.table(vn_r,file="tcgar矩阵.txt",sep="\t",quote=F,col.names=T)
write.table(vn_p,file="tcgap矩阵.txt",sep="\t",quote=F,col.names=T)
##相关性热图
corr <- as.matrix(r)
pvalue <- as.matrix(p)
# 检查相关性矩阵和P值矩阵的维度
dim(corr)
dim(pvalue)
pvalue[is.na(pvalue)] <- 1
library(corrplot)

addcol <- colorRampPalette(c("red", "white", "blue"))
#绘图并保存
#pdf("tcgaplot.pdf", width = 8, height = 8) 
#corrplot(corr, # 相关性矩阵
#         method = "color", # 表示用颜色表示相关性大小
#         col = addcol(100), 
#         tl.col = "black", # 文本标签的颜色
#         tl.cex = 0.8, # 文本标签的字符大小
#         tl.srt = 90, #  文本标签的旋转角度
#         tl.pos = "td", # 文本标签位置，td表示顶部和对角线 
#         p.mat = pvalue, #  P 值矩阵
#         diag = T, # 是否显示对角线上的相关性值
#         type = 'upper', # 只绘制上三角部分
#         sig.level = c(0.05), # 设置显著性水平阈值，可设置多个
#         pch.cex = 1,  # 显著性标记字符大小
#         pch.col = 'grey20',  # 显著性标记字符颜色
#         insig = 'label_sig',
#         order = 'AOE', #设置一种排序方式
#)
#dev.off()
mergedf$Size <- ifelse(mergedf$Value.1 < 0.05, 3, 1) # 小于0.05的size较大

# 绘图
pdf("tcga.pdf", width = 12, height = 8) 
p1 <- ggplot(mergedf, aes(x = Variable, y = Gene)) +
  geom_point(aes(size = Size, 
                 color = Value)) +
  theme_bw() +
  theme(panel.grid = element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
  scale_color_gradient2(low = "blue", mid = "red", high = "red", midpoint = 0.5, 
                        limits = c(0.1, 0.5), na.value = "grey") +
  labs(x = NULL, y = NULL)
p1
dev.off()



library(corrplot)
cggavn <- read.table("cgga交集基因fpkms.txt",sep = "\t",check.names = F,stringsAsFactors = F,header = T)
cggaupr <- read.table("cggauprmt-er.txt",sep = "\t",check.names = F,stringsAsFactors = F,header = T)

cggavn <- t(cggavn)
comgene <- intersect(rownames(cggaupr),rownames(cggavn))
cggavn <- cggavn[comgene,]
cggaupr <- cggaupr[comgene,]

cggauprmt_vn=cbind(cggaupr, cggavn)
library(Hmisc)#加载包
res2 <- rcorr(as.matrix(cggauprmt_vn)) ##可以看到这个list里包含三个，r为相关性分数，p为pvalue
r <- res2$r
p <- res2$P
vn_r <- r[colnames(cggavn),]
vn_r <- vn_r[,colnames(cggaupr)]
vn_p <- p[colnames(cggavn),]
vn_p <- vn_p[,colnames(cggaupr)]
library(tidyr)
library(dplyr)
library(tibble)
# 假设你的数据框名为df
df <- as.data.frame(vn_r) %>%           # 将vn_r转换为数据框
  rownames_to_column("Gene") %>%        # 将行名转换为Gene列
  pivot_longer(-Gene,                   # 将Gene列之外的所有列都转换为长格式
               names_to = "Variable",   # 新列的名称，用来存储原列名
               values_to = "Value")     # 新列的名称，用来存储原数值

df2 <- as.data.frame(vn_p) %>%           # 将vn_r转换为数据框
  rownames_to_column("Gene") %>%        # 将行名转换为Gene列
  pivot_longer(-Gene,                   # 将Gene列之外的所有列都转换为长格式
               names_to = "Variable",   # 新列的名称，用来存储原列名
               values_to = "Value")     # 新列的名称，用来存储原数值
mergedf <- cbind(df,df2)
mergedf <- mergedf[,-4]
mergedf <- mergedf[,-4]
# 查看转换后的数据框
head(df)


write.table(vn_r,file="cggar矩阵.txt",sep="\t",quote=F,col.names=T)
write.table(vn_p,file="cggap矩阵.txt",sep="\t",quote=F,col.names=T)
##相关性热图
corr <- as.matrix(r)
pvalue <- as.matrix(p)
# 检查相关性矩阵和P值矩阵的维度
dim(corr)
dim(pvalue)
pvalue[is.na(pvalue)] <- 1
library(corrplot)

addcol <- colorRampPalette(c("red", "white", "blue"))
#绘图并保存
pdf("cggaplot.pdf", width = 8, height = 8) 
corrplot(corr, # 相关性矩阵
         method = "color", # 表示用颜色表示相关性大小
         col = addcol(100), 
         tl.col = "black", # 文本标签的颜色
         tl.cex = 0.8, # 文本标签的字符大小
         tl.srt = 90, #  文本标签的旋转角度
         tl.pos = "td", # 文本标签位置，td表示顶部和对角线 
         p.mat = pvalue, #  P 值矩阵
         diag = T, # 是否显示对角线上的相关性值
         type = 'upper', # 只绘制上三角部分
         sig.level = c(0.05), # 设置显著性水平阈值，可设置多个
         pch.cex = 1,  # 显著性标记字符大小
         pch.col = 'grey20',  # 显著性标记字符颜色
         insig = 'label_sig',
         order = 'AOE', #设置一种排序方式
)
dev.off()

library(ggplot2)
#pdf("cgga.pdf", width = 12, height = 8) 
#p1<-ggplot(mergedf,aes(x=Variable,y=Gene))+
  geom_point(aes(size=`Value.1`,
                 color=`Value`))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text.x=element_text(angle=90,hjust = 1,vjust=0.5))+
  scale_color_gradient(low="red",high="blue")+
  labs(x=NULL,y=NULL)
#p1
#dev.off()
mergedf$Size <- ifelse(mergedf$Value.1 < 0.05, 3, 1) # 小于0.05的size较大

# 绘图
pdf("cgga.pdf", width = 12, height = 8) 
p1 <- ggplot(mergedf, aes(x = Variable, y = Gene)) +
  geom_point(aes(size = Size, 
                 color = Value)) +
  theme_bw() +
  theme(panel.grid = element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
  scale_color_gradient2(low = "blue", mid = "red", high = "red", midpoint = 0.5, 
                        limits = c(0.1, 0.5), na.value = "grey") +
  labs(x = NULL, y = NULL)
p1
dev.off()

